package com.hqully.smartfumehood.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqully.smartfumehood.common.utils.UserUtil;
import com.hqully.smartfumehood.common.vo.QueryVo;
import com.hqully.smartfumehood.entity.LabManager;
import com.hqully.smartfumehood.entity.User;
import com.hqully.smartfumehood.entity.dto.LabManagerDto;
import com.hqully.smartfumehood.mapper.LabManagerMapper;
import com.hqully.smartfumehood.mapper.UserMapper;
import com.hqully.smartfumehood.service.LabManagerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *     实验室管理员服务实现类
 * </p>
 * @author liulingyu
 * @date 2022-05-08 14:40
 * @version 1.0
 */
@Service
public class LabManagerServiceImpl extends ServiceImpl<LabManagerMapper, LabManager> implements LabManagerService {

    @Autowired
    private LabManagerMapper  labManagerMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<LabManager> findAllLabManager(QueryVo queryVo) {
        PageHelper.startPage(queryVo.getPageNum(),queryVo.getPageSize(),true);

        List<LabManager> labManagerList = labManagerMapper.findAllLabManager(queryVo.getQuery());

        PageInfo<LabManager> labManagerPageInfo = new PageInfo<>(labManagerList);
        return labManagerPageInfo;
    }

    @Override
    public PageInfo<LabManager> findLabManagerByUnit(QueryVo queryVo) {
        PageHelper.startPage(queryVo.getPageNum(),queryVo.getPageSize(),true);

        List<LabManager> labManagerList = labManagerMapper.findLabManagerByUnit((String) queryVo.getQuery().get("queryInfo"));

        PageInfo<LabManager> labManagerPageInfo = new PageInfo<>(labManagerList);
        return labManagerPageInfo;
    }

    @Override
    public LabManager findLabManagerByLid(String lid) {
        return labManagerMapper.findLabManagerByLid(lid);
    }

    @Override
    public LabManager findLabManagerByPhone(String phone) {
        return labManagerMapper.findLabManagerByPhone(phone);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addLabManager(LabManagerDto labManagerDto) {
        User labManagerUser =User.builder()
                .id(labManagerDto.getId())
                .username(labManagerDto.getUsername())
                .password(labManagerDto.getPassword())
                .build();

        User secretUser = UserUtil.register(labManagerUser);

        int i = userMapper.addUser(secretUser);

        userMapper.bindRole(labManagerUser.getId(), 1);

        labManagerMapper.addLabManager(labManagerDto);

        return i;
    }

    @Override
    public int deleteLabManager(String lid) {
        return labManagerMapper.deleteLabManager(lid);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateLabManager(LabManagerDto labManagerDto) {
        User labManagerUser =User.builder()
                .id(labManagerDto.getId())
                .username(labManagerDto.getUsername())
                .password(labManagerDto.getPassword())
                .build();

        User secretUser = UserUtil.register(labManagerUser);

        int status1 = userMapper.updateUser(secretUser);
        int status2 = labManagerMapper.updateLabManager(labManagerDto);
        return status1 & status2;
    }
}
